#include "Minion.hpp"
#include <GL\freeglut.h>
#include "siut/simd/Vec4f.hpp"

using siut::simd::Vec4f;


Minion::Minion()
{
  life_ = 0;
  //parentGameManager_ = 0x0;
}

Minion::~Minion()
{
}

void Minion::privateInit()
{

}

//! if alive, then render.
void Minion::privateRender()
{
  if(life_ > 0)
  {
	glColor3f(1.0f, 1.0f, 0.0f);
	float size = 5.0f;
	glutWireSphere(size, 10, 10);

	// Indicate life amount
	glPushMatrix();
	float s = float(life_) / 5.0f;
	glTranslatef(0,10.0f,0);
	glScalef( size,s,size );
	glRotatef(90,0,0,1);
	glutWireTetrahedron();
	//glutWireSphere( float(life_)/20.0f, 4, 4);
	glPopMatrix();
  }
}

//! if alive, position minion using waypoints_ 
void Minion::privateUpdate()
{
  if( isAlive() )
  {
	// keep minion on the tracks ;)
	// take the start and end waypoints and interpolate between the two. Animate using deltatime
	Vec3f beginPos( *waypoints_[nextWP_-1] );
	Vec3f endPos( *waypoints_[nextWP_] );

  	float lineLength = euclideanDistance(beginPos,endPos);
	pointOnLine += 25.0f*float(sslf_) * 1/lineLength; // keep speed consistent, div by lineLength

	Vec3f newPos = lerp(beginPos, endPos, pointOnLine);
	this->setPosition( newPos );

	// reached end of segment, go to next
  	if( pointOnLine > 1.0f ){
	  pointOnLine = 0.0f;
	  nextWP_++;
	  if(nextWP_ > int(waypoints_.size()-1) ) nextWP_ = 1; // completed track, goto start.
	}
  }
}

void Minion::setAlive()
{
  life_ = 100;
  nextWP_ = 1;
  pointOnLine = 0.0f;
  setPosition( *waypoints_[nextWP_-1] );
}

bool Minion::isDead()
{
  return life_ <= 0;
}

bool Minion::isAlive()
{
  return (life_ > 0);
}

void Minion::setWaypoints( std::vector< boost::shared_ptr<Vec3f> > wp )
{
  waypoints_ = wp;
}

void Minion::takeDamage()
{
  life_ -= 5; // 100hp / 5hp = 20 hits before killed
}


